<?php

namespace Appstore\Model;


use Think\Model;

class AppstoreVersionModel extends Model
{

    /**获取安装指纹
     * @param $id 版本号
     * @param $uid 用户ID
     * @return bool
     */
    public function getToken($id, $uid)
    {
        if (!$uid) {
            $this->error = '未设置账号';
            return false;
        }

        $version = $this->find($id);
        //如果是收费的，进行付款验证
        if (floatval($version['need_buy']) != 0) {
            $orderModel = D('AppstoreOrder');
            if (!$orderModel->isPayed($id, $uid)) {
                $this->error = '未购买商品，无法安装。';
                return false;
            }
        }
        $tokenModel = M('AppstoreToken');
        $oldToken = $tokenModel->where(array('version_id' => $id, 'uid' => $uid, 'status' => 1))->find();
        if ($oldToken) {
            return $oldToken;
        }

        $token['token'] = md5(md5('uid_' . $uid . '_vid_' . $id . '_' . time()) . time());
        $token['create_time'] = time();
        $token['version_id'] = $id;
        $token['uid'] = $uid;
        $token['goods_id'] = $version['goods_id'];
        $token['status'] = 1;
        $result = $tokenModel->add($token);
        if ($result === false) {
            $this->error = '生成安装指纹失败，数据库写入失败。';
            return false;
        }
        return $token;

    }

    public function formatLog($log)
    {
        $log = explode("\r\n", $log);
        $log = '<li>' . implode('</li><li>', $log) . '</li>';
        return $log;
    }

    /**
     * @param $token
     * @param bool $use
     * @param string $type current当前token关联的版本，next当前token关联的版本的下一个版本
     * @return bool
     */
    public function getVersionByToken($token, $use = false,$type='current')
    {
        $tokenModel = M('AppstoreToken');
        $token = $tokenModel->where(array('token' => $token, 'status' => 1))->find();
        if (!$token) {
            $this->error = 'token不存在。';
            return false;
        }

        $versions = $this->where(array('id' => array('egt', intval($token['version_id'])), 'goods_id' => $token['goods_id'],'status'=>1))->order('id asc')->select();

        if($type=='next'){
            $version=$versions[1];
        }else{
            $version=$versions[0];
        }
        if (!$version) {
            $this->error = '版本不存在。';
            return false;
        }
        if ($use===true) {
            $token['use_time'] = time();
            $token['status'] = 0;
            $rs = $tokenModel->save($token);
            if ($rs === false) {
                $this->error = '将Token设为已经使用失败。';
                return false;
            }
        }


        $version['goods'] = D('AppstoreGoods')->getById($version['goods_id']);
        return $version;
    }

    /**
     * 获取版本列表
     * @param $map
     * @param $order
     * @return mixed
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getVersionList($map, $order = 'id desc')
    {
        $list = $this->where($map)->order($order)->select();
        return $list;
    }

    /**获取更新列表
     * @param $token
     * @return bool|mixed
     */
    public function getUpdateListByToken($token)
    {
        $version = $this->getVersionByToken($token);
        $token=M('AppstoreToken')->where(array('token'=>$token))->find();
        if (!$version) {
            $this->error = '版本不存在。';
            return false;
        }

        $versions = $this->where(array('id' => array('gt', intval($version['id'])), 'goods_id' => $version['goods_id'],'status'=>1))->order('id desc')->select();
        foreach($versions as $key=>&$v){
            $v['token']=$this->getToken($v['id'],$token['uid']);
            if($v['token']===false){
                unset($versions[$key]);
            }
        }
        unset($v);
        return $versions;

    }

    /**获取版本
     * @param $id
     * @return mixed
     */
    public function getVersion($id)
    {
        $version = $this->find($id);
        return $version;
    }
}